package com.study.chunguard.common.enums;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 环境类型枚举
 * @author weixiaochun
 * @date 2024-03-06
 */
public enum ProfileTypeEnum {

    /**
     * 开发环境
     */
    DEV("dev", "开发"),

    /**
     * 测试环境
     */
    TEST("test", "测试"),

    /**
     * 生产环境
     */
    PROD("prod", "生产"),

    /**
     * 麒麟生产环境
     */
    PROD_KYLIN("prod_kylin", "麒麟生产");

    /**
     * 代码
     */
    private final String code;

    /**
     * 名称
     */
    private final String name;

    private static final Map<String, ProfileTypeEnum> CODE_TO_ENUM = Arrays.stream(ProfileTypeEnum.values())
            .collect(Collectors.toMap(ProfileTypeEnum::getCode, Function.identity()));

    private static final Map<String, ProfileTypeEnum> NAME_TO_ENUM = Arrays.stream(ProfileTypeEnum.values())
            .collect(Collectors.toMap(ProfileTypeEnum::getName, Function.identity()));

    ProfileTypeEnum(String code, String name) {
        this.code = code;
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public String getName() {
        return name;
    }

    public static ProfileTypeEnum of(String code) {
        return Optional.ofNullable(CODE_TO_ENUM.get(code))
                .orElseThrow(() -> new IllegalArgumentException("环境类型枚举错误"));
    }

    public static String getCodeByName(String name) {
        return Optional.ofNullable(NAME_TO_ENUM.get(name))
                .map(ProfileTypeEnum::getCode)
                .orElseThrow(() -> new IllegalArgumentException("环境类型枚举错误"));
    }
}
